package org.ph.share.lock.test;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedDemo2 {
    private int count = 0;

    private final List<Integer> list = new ArrayList<>();

    public synchronized void increment() {
        count++;
    }

    public synchronized void add(int num) {
        list.add(num);
        count += num;
    }

    public synchronized void decrement() {
        count--;
    }

    public synchronized int getCount() {
        return count;
    }

    public static void main(String[] args) throws InterruptedException {
        SynchronizedDemo2 demo = new SynchronizedDemo2();
        ExecutorService executor = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 10; i++) {
            executor.submit(() -> {
                Random random = new Random();
                int num = random.nextInt(10) + 1;
                // int num = 1;
                log.info("num:{}", num);
                demo.add(num);
                log.info("demo.getCount():{}", demo.getCount());
            });
        }
        executor.shutdown();

    }
}
